package com.longshen.dubbo.api.restful;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.longshen.dubbo.api.anno.LoginRequired;
import com.longshen.dubbo.api.base.ErrorCode;
import com.longshen.dubbo.api.base.RestResult;
import com.longshen.dubbo.api.model.AdminRole;
import com.longshen.dubbo.api.model.AdminRoleMenu;
import com.longshen.dubbo.api.model.AdminTrade;
import com.longshen.dubbo.api.restful.vo.AddTradesIn;
import com.longshen.dubbo.api.restful.vo.ModeId;
import com.longshen.dubbo.api.restful.vo.RoleListOut;
import com.longshen.dubbo.api.restful.vo.RolePageIn;
import com.longshen.dubbo.api.restful.vo.RoleTradeVo;
import com.longshen.dubbo.api.restful.vo.RoleVO;
import com.longshen.dubbo.api.restful.vo.TradeVO;
import com.longshen.dubbo.api.service.AdminRoleService;
import com.longshen.dubbo.api.service.AdminRoleTradeService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequestMapping(value = "/role")
@RestController
@LoginRequired
public class AdminRoleController {
	@Autowired
	private AdminRoleService adminRoleService;
	
	
	@Autowired
	private AdminRoleTradeService adminRoleTradeService;

	// 分页查询
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public RestResult list(@RequestBody RolePageIn in) {

		Page<AdminRole> pages = adminRoleService.queryPage(in.getPagesize(), in.getPagenum(), in.getRolename());
		if (pages != null && pages.getPageSize() > 0) {
			RoleListOut out = new RoleListOut();
			List<RoleVO> list = new ArrayList<RoleVO>();
			for (AdminRole role : pages) {
				RoleVO vo = new RoleVO();
				vo.setFlag(role.getFlag());
				vo.setId(role.getId());
				vo.setRoledesc(role.getRoleDesc());
				vo.setRolename(role.getRoleName());
				list.add(vo);
			}
			out.setCurrentsize(pages.getPageSize());
			out.setTotalsize((int) pages.getTotal());
			out.setList(list);
			RestResult rest = RestResult.success();
			rest.setData(out);
			return rest;
		} else {
			RestResult rest = RestResult.newInstance();
			rest.setErrorCode(ErrorCode.E_000004);
			return rest;
		}
	}
	// 添加

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public RestResult add(@RequestBody RoleVO in) {
		try {

			AdminRole add = new AdminRole();
			add.setFlag(in.getFlag());
			add.setRoleDesc(in.getRoledesc());
			add.setRoleName(in.getRolename());
			adminRoleService.add(add, in.getMenus());

			return RestResult.success();
		} catch (DuplicateKeyException e) {
			log.error("添加角色异常", e);
			RestResult rest = RestResult.newInstance();
			rest.setErrorCode(ErrorCode.E_000006);
			rest.setMessage(rest.getMessage() + in.getRolename());
			return rest;
		} catch (Exception e) {
			return RestResult.fail(ErrorCode.E_999999);
		}
	}

	// 删除
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public RestResult del(@RequestBody ModeId in) {

		adminRoleService.delById(in.getId());
		return RestResult.success();
	}

	// 修改
	@RequestMapping(value = "/mod", method = RequestMethod.POST)
	public RestResult mod(@RequestBody RoleVO in) {

		AdminRole mod = new AdminRole();
		mod.setFlag(in.getFlag());
		mod.setRoleDesc(in.getRoledesc());
		mod.setId(in.getId());
		mod.setRoleName(in.getRolename());
		adminRoleService.mod(mod, in.getMenus());
		return RestResult.success();
	}

	@RequestMapping(value = "/menus", method = RequestMethod.POST)
	public RestResult menus(@RequestBody ModeId in) {
		List<AdminRoleMenu> roleMenus = adminRoleService.roleMenus(in.getId());
		List<String> voList = new ArrayList<String>();
		// 添加更目录
		if (roleMenus != null) {
			roleMenus.forEach(m -> {
				;
				voList.add(m.getMenuId());
			});
		}
		RestResult rest = RestResult.success();
		rest.setData(voList);
		return rest;

	}

	@RequestMapping(value = "/queryid", method = RequestMethod.POST)
	public RestResult queryById(@RequestBody ModeId in) {

		AdminRole role = adminRoleService.queryById(in.getId());
		if (role != null) {
			RoleVO vo = new RoleVO();
			vo.setId(role.getId());
			vo.setFlag(role.getFlag());
			vo.setRoledesc(role.getRoleDesc());
			vo.setRolename(role.getRoleName());

			List<AdminRoleMenu> roleMenus = adminRoleService.roleMenus(in.getId());
			List<String> menus=new ArrayList<String>();
			if(roleMenus !=null) {
				roleMenus.forEach(r->{
					menus.add(r.getMenuId());
				});
				vo.setMenus(menus);
			}
			return RestResult.success(vo);
		} else {
			RestResult rest = RestResult.newInstance();
			rest.setErrorCode(ErrorCode.E_000004);
			return rest;

		}
	}
	
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public RestResult all() {

		List<AdminRole> all = adminRoleService.all();
		if (all != null) {
			List<RoleVO> voList=new ArrayList<RoleVO>();
			
			all.forEach(a->{
				RoleVO vo =new RoleVO();
				vo.setId(a.getId());
				vo.setRolename(a.getRoleName());
				voList.add(vo);
			});
			return RestResult.success(voList);
		} else {
			return RestResult.success();

		}
	}
	/**
	 * 
	 * @Title: OwnTrade   
	 * @Description: 角色拥有交易列表 
	 * @return      
	 * @return: RestResult      
	 * @throws
	 */
	@RequestMapping(value = "/owntrade", method = RequestMethod.POST)
	public RestResult OwnTrade(@RequestBody ModeId in) {

		List<AdminTrade> all = adminRoleService.ownTreade(in.getId());
		if (all != null) {
			List<TradeVO> voList=new ArrayList<TradeVO>();
			
			all.forEach(t->{
				TradeVO vo = new TradeVO();

				vo.setFlag(t.getFlag());
				vo.setId(t.getId());
				vo.setTradecode(t.getTradeCode());
				vo.setTradedesc(t.getTradeDesc());
				vo.setTradename(t.getTradeName());
				voList.add(vo);
			});
			return RestResult.success(voList);
		} else {
			return RestResult.success();

		}
	}
	/**
	 * 
	 * @Title: OwnTrade   
	 * @Description: 角色拥有交易列表 
	 * @return      
	 * @return: RestResult      
	 * @throws
	 */
	@RequestMapping(value = "/unowntrade", method = RequestMethod.POST)
	public RestResult UnOwnTrade(@RequestBody ModeId in) {
		List<AdminTrade> all = adminRoleService.UnOwnTreade(in.getId());
		if (all != null) {
			List<TradeVO> list=new ArrayList<TradeVO>();
			
			all.forEach(t->{
				TradeVO vo = new TradeVO();

				vo.setFlag(t.getFlag());
				vo.setId(t.getId());
				vo.setTradecode(t.getTradeCode());
				vo.setTradedesc(t.getTradeDesc());
				vo.setTradename(t.getTradeName());
				list.add(vo);
			});
			return RestResult.success(list);
		} else {
			return RestResult.success();

		}
	}
	@RequestMapping(value = "/addtrades", method = RequestMethod.POST)
	public RestResult  addTrades(@RequestBody AddTradesIn in) {
		adminRoleTradeService.addTrades(in.getRoleid(), in.getTrades());
		return RestResult.success();
		
	}
	
	@RequestMapping(value = "/deltrade", method = RequestMethod.POST)
	public RestResult  deltrade(@RequestBody RoleTradeVo in) {
		adminRoleTradeService.dellRoleTrade(in.getRoleid(),in.getTradeid());
		return RestResult.success();
		
	}
}
